﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GetSong : MonoBehaviour {
    [HideInInspector]
    public string m_strSong;//乐谱,读表获取
    string m_strName;
    int index;
    bool isFirst = true;
    float BetweenSphere;//两球之间距离
    float betweensphere;
    Transform inst;
    Vector3 StartPos;
    SphereTranslate sphere;
    private void Awake()
    {
        BetweenSphere = GlobalHelper.MainGame.m_fSphereBetween/100;
    }
    public void OnStart()
    {
        StartCoroutine(WaitDistance());//等两球的距离
    }
    IEnumerator WaitDistance()
    {
        for (int i = 0; i < m_strSong.Length; i++)
        {
            //实例化相应小球
            m_strName = m_strSong[i].ToString();
            index = (int)m_strSong[i];
            if (32 == index || 48 == index) { }
            else
            {
                if (!isFirst && 32 == (int)m_strSong[i - 1])
                {
                    betweensphere =2* BetweenSphere;
                }
                else
                {
                    betweensphere = BetweenSphere;
                }
                if (isFirst)
                {
                    //只能进一次
                    isFirst = false;
                    //实例化相应小球//加入列表//加Translate脚本                
                    Transform trans = Resources.Load<Transform>("Sphere/" + m_strName);
                    StartPos = Game1.m_vStartPos[index - 49];
                    inst = GlobalHelper.SpherePool.Spawn(trans,new Vector3 (0.6f,0.6f,0.6f),StartPos);
                    Game1.isStart = true;
                    Game1.SphereList.Add(inst);
                    inst.gameObject.GetOrAddComponent<SphereTranslate>();
                }
                else
                {
                    yield return new WaitUntil(() => (StartPos.y - inst.position.y >= betweensphere));
                    Transform trans = Resources.Load<Transform>("Sphere/" + m_strName);
                    StartPos = Game1.m_vStartPos[index - 49];
                    inst = GlobalHelper.SpherePool.Spawn(trans, new Vector3(0.6f, 0.6f, 0.6f), StartPos);
                    sphere = inst.gameObject.GetComponent<SphereTranslate>();
                    Game1.isStart = true;
                    Game1.SphereList.Add(inst);
                    inst.gameObject.GetOrAddComponent<SphereTranslate>();
                    if (m_strSong.Length - 1 == i)
                    {
                        sphere.m_bFinal = true;
                    }
                }
            }           
        }
    }
}
